package com.zy.fee.roadpark.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import com.flyco.roundview.RoundTextView;
import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.dao.CommonLogic;
import com.zygk.library.base.BaseActivity;
import com.zygk.library.util.ActivityCollector;
import com.zygk.library.util.ColorUtil;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.PreferencesHelper;
import com.zygk.library.util.Security;
import com.zygk.library.util.StringUtils;
import com.zygk.library.util.ToastUtil;

/**
 * Created by Wikison on 2019/7/17.
 * 修改密码
 */
public class ModifyPasswordActivity extends BaseActivity {
  @BindView(R.id.lh_tv_title) TextView lhTvTitle;
  @BindView(R.id.et_origin_pwd) EditText etOriginPwd;
  @BindView(R.id.et_new_pwd) EditText etNewPwd;
  @BindView(R.id.et_new_pwd_again) EditText etNewPwdAgain;
  @BindView(R.id.tv_button) RoundTextView tvButton;

  private String password = "", strPwdNew = "", strPwdNewAgain = "";

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
  }

  @Override public void setContentView() {
    setContentView(R.layout.activity_modify_password);
  }

  @Override public void init() {
    initView();

    initListener();
  }

  private void initView() {
    lhTvTitle.setText("修改密码");
  }

  private void initListener() {
    etOriginPwd.addTextChangedListener(textWatcher);
    etNewPwd.addTextChangedListener(textWatcher);
    etNewPwdAgain.addTextChangedListener(textWatcher);
  }

  private TextWatcher textWatcher = new TextWatcher() {
    @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
      if (StringUtils.isBlank(etOriginPwd.getText().toString()) || StringUtils.isBlank(
          etNewPwd.getText().toString()) || StringUtils.isBlank(
          etNewPwdAgain.getText().toString())) {
        tvButton.getDelegate().setBackgroundColor(ColorUtil.getColor(mContext, R.color.font_black_999));
      }else {
        tvButton.getDelegate().setBackgroundColor(ColorUtil.getColor(mContext, R.color.theme_color));
      }
    }

    @Override public void afterTextChanged(Editable s) {

    }
  };

  @OnClick({
      R.id.ll_back, R.id.tv_button
  }) public void onViewClicked(View view) {
    Intent intent;
    switch (view.getId()) {
      case R.id.ll_back:
        finish();
        break;
      case R.id.tv_button:
        password = etOriginPwd.getText().toString();
        strPwdNew = etNewPwd.getText().toString();
        strPwdNewAgain = etNewPwdAgain.getText().toString();
        if (StringUtils.isBlank(password)) {
          ToastUtil.showMessage(mContext, "原密码不能为空");
          return;
        }
        if (StringUtils.isBlank(strPwdNew)) {
          ToastUtil.showMessage(mContext, "新密码不能为空");
          return;
        }
        if (StringUtils.isBlank(strPwdNewAgain)) {
          ToastUtil.showMessage(mContext, "再次输入不能为空");
          return;
        }
        if (!strPwdNewAgain.equals(strPwdNew)) {
          ToastUtil.showMessage(mContext, "新密码与再次输入的不一致");
          return;
        }
        if (strPwdNew.length() < 6) {
          ToastUtil.showMessage(mContext, "密码不得小于6位");
          return;
        }
        login();
        break;
    }
  }

  //验证旧密码是否正确
  private void login() {
    CommonLogic.login(mContext,
        preferencesHelper.getSettingString(PreferencesHelper.User.Key.USER_NAME),
        Security.encryptByMD5(password), new HttpRequest.HttpCallback() {
          @Override public void onStart() {

          }

          @Override public void onSucceed(Object obj) {
            password_edit();
          }

          @Override public void onFailed() {
            ToastUtil.showMessage(mContext, "原密码输入错误");
          }

          @Override public void onFinish() {

          }
        });
  }

  private void password_edit() {
    CommonLogic.password_edit(mContext, preferencesHelper.userManager().getUserID(),
        Security.encryptByMD5(strPwdNew), new HttpRequest.HttpCallback() {
          @Override public void onStart() {
            showPd();
          }

          @Override public void onSucceed(Object obj) {
            preferencesHelper.clearData();
            ToastUtil.showMessage(mContext, "密码修改成功, 请重新登录");
            JPushInterface.deleteAlias(mContext, 1);
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            ActivityCollector.finishAll();
          }

          @Override public void onFailed() {

          }

          @Override public void onFinish() {
            dismissPd();
          }
        });
  }
}
